#install.packages("ggplot2")
#install.packages("viridis")
#install.packages("latticeExtra")

require(ggplot2)
library(viridis)
library(latticeExtra)
library(reshape2)
#install.packages("ggsci")
library(ggsci)

### aspect eps 656 x 468
### 60 0 425 351
d<- read.csv(file="dataJPN.csv",head=TRUE)
df<- data.frame(value=d)
############################  ###################################
pp<- ggplot(df, aes(sample = value.hplngdp) )+
  stat_qq() + 
  #stat_qq_line(line.p = c(0.001, 0.999)) +
  stat_qq_line() +
  theme(aspect.ratio = 1) +
  ylab("HP-detrended  ln GDP_JP") +
  xlab("Normal theoretical quantiles")+coord_fixed()#this fixes the coordinates
pp
ggsave(file = "HPlnGDPJP.eps", plot = pp, width = 6.56, height = 4.68)
###############################################################
d2<- read.csv(file="dataUSA.csv",head=TRUE)
df2<- data.frame(value=d2)
############################  ###################################
p2<- ggplot(df2, aes(sample = value.hplngdp) )+
  stat_qq() + 
  stat_qq_line(line.p = c(0.25, 0.75)) +
#  stat_qq_line() +
  theme(aspect.ratio = 1) +
  ylab("HP-detrended  ln GDP_US") +
  xlab("Normal theoretical quantiles")+coord_fixed()#this fixes the coordinates
p2
ggsave(file = "HPlnGDPUS.eps", plot = p2, width = 6.56, height = 4.68)
############################  ###################################
d3<- read.csv(file="dataJPN_recent.csv",head=TRUE)
df3<- data.frame(value=d3)
############################  ###################################
make_qq <- function(dd, x) {
  dd<-dd[order(dd[[x]]), ]
  dd$qq <- qnorm(ppoints(nrow(dd)))
  dd
}

p3<- ggplot(make_qq(d3, "hplngdp")) + 
  geom_point(aes(x=qq, y=hplngdp, shape=as.factor(GFC))) + 
  scale_shape_manual(values = c(16, 1)) + 
  theme(aspect.ratio = 1, legend.position = 'none') +
  geom_abline(slope = 0.0145, intercept = -0.0015 )+
  #stat_qq_line(line.p = c(0.25, 0.75)) +
  ylab("HP-detrended ln GDP_JP (1994I - 2015IV)") +
  xlab("Normal theoretical quantiles")+coord_fixed()#this fixes the coordinates
  #labs(x="Theoretical",y="Observed") 
p3
ggsave(file = "HPlnGDPJPrecent.eps", plot = p3, width = 6.56, height = 4.68)
##########################################
d4<- read.csv(file="dataUSA_recent.csv",head=TRUE)
df4<- data.frame(value=d4)
############################  ###################################
make_qq <- function(dd, x) {
  dd<-dd[order(dd[[x]]), ]
  dd$qq <- qnorm(ppoints(nrow(dd)))
  dd
}

p4<- ggplot(make_qq(d4, "hplngdp")) + 
  geom_point(aes(x=qq, y=hplngdp, shape=as.factor(GFC))) + 
  scale_shape_manual(values = c(16, 1)) + 
  theme(aspect.ratio = 1, legend.position = 'none') +
  geom_abline(slope = 0.015, intercept = 0.001 )+
  #stat_qq_line(line.p = c(0.25, 0.75)) +
  ylab("HP-detrended ln GDP_US (1997I - 2020IV)") +
  xlab("Normal theoretical quantiles")+coord_fixed()#this fixes the coordinates
#labs(x="Theoretical",y="Observed") 
p4
ggsave(file = "HPlnGDPUSrecent.eps", plot = p4, width = 6.56, height = 4.68)
##########################################
















  
p3<- ggplot(df3, aes(sample = value.hplngdp) )+
#  scale_shape_manual(values = c(15, 16)) + 
#  theme_minimal() +
  stat_qq() + 
#  geom_point(aes(shape = as.factor(value.GFC)), size = 3) + 
      stat_qq_line(line.p = c(0.27, 0.83)) +
  #  stat_qq_line() +
  theme(aspect.ratio = 1) +
  ylab("HP-detrended  ln GDP_US") +
  xlab("Normal theoretical quantiles")+coord_fixed()#this fixes the coordinates
p3
ggsave(file = "HPlnGDPUS.eps", plot = p3, width = 6.56, height = 4.68)
############################  ###################################



